from Interpolation import Interpolation, run

class Lagrange(Interpolation):
    def addKey(self, x, y):
        super().addKey(x, y)
        def L(k, x):
            prod = 1
            for i, point in enumerate(self.keys):
                if i==k: continue
                prod *= (x-point.x)/(self.keys[k].x-point.x)
            return prod
        def P(x):
            sum = 0
            for i, point in enumerate(self.keys):
                sum += point.y * L(i, x)
            return sum
        self.compute = P

run(Lagrange())